$(document).ready(function() {
    if (typeof(Storage) !== "undefined")
    {

        $('.wd-loading').show();
        getGuestList();

        //paging
        var killScroll = false;
        $(window).scroll(function() {
            if ($('#end_paging').val() == 0) {
                if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                    if (killScroll == false) {
                        killScroll = true;
                        $('.wd-loading').show();
                        $.post(localStorage.domain_app + "/webservice/guest/getEventInviteGuest.php", {
                            page: $('#page').val(),
                            event_id: sessionStorage.event_edit_id,
                            user_id: localStorage.userID
                        }, function(res) {
                            $('#wd-list-guest-event').append(res.html);
                            $('#page').val(res.page);
                            $('#end_paging').val(res.end_paging);
                            checkAll();
                            eventClickCheckbox();
                            //uncheck all
                            $('#wd-checkall').removeAttr('checked');
                            killScroll = false;
                            $('.wd-loading').hide();
                        }, "json");
                    } else {
                        return false;
                    }
                }
            }
        });
        var invite = false;
        $('#act-invite-guest-event').click(function() {
            if (invite == false) {
                invite = true;
                var guest_list = new Array();
                var count = 0;
                $('.checkbox-invite-guest').each(function() {
                    if ($(this).is(':checked')) {
                        guest_list[count] = $(this).attr('rel');
                        count++;
                    }
                });
                if (count > 0) {
                    $.post(localStorage.domain_app + "/webservice/guest/inviteGuestForEvent.php", {
                        user_id: localStorage.userID,
                        event_id: sessionStorage.event_edit_id,
                        guest_list: guest_list
                    }, function(res) {
                        if (res.error == 1) {
                            print_alert(res.msg);
                        } else {
                            print_alert(res.msg);
                            $('.wd-loading').show();
                            getGuestList();
                        }
                        invite = false;
                    }, "json");
                }else{
                    invite = false;
                    print_alert("Please select guest(s)!");
                    return false;
                }
            }
        });


    }
});
function checkAll() {
    $('.checkall-friends').click(function() {
        var checkbox_all = $('#' + $(this).attr('for')).is(':checked');
        if (checkbox_all) {
            $('#wd-list-guest-event .regular-checkbox-addguest').removeAttr('checked');
        } else {
            $('#wd-list-guest-event .regular-checkbox-addguest').attr('checked', 'checked');
        }

    });
}
function eventClickCheckbox() {
    $('.label-cbx-invite').click(function() {
        var checkbox_current_id = $(this).attr('for');
        var checkbox_all = $('#wd-checkall').is(':checked');
        var checkbox_item = $('#' + $(this).attr('for')).is(':checked');
        if (checkbox_item) {
            if (checkbox_all) {
                $('#wd-checkall').removeAttr('checked');
            }

        } else {
            var is_check_all = true;
            $('input.regular-checkbox-addguest', "#wd-list-guest-event").each(function() {
                if (!this.checked && $(this).attr("rel") != $("#" + checkbox_current_id).attr("rel")) {
                    is_check_all = false;

                }
            });
            if (is_check_all) {
                $('#wd-checkall').attr('checked', 'checked');
            }
        }


    });
}
function getGuestList() {
    $('#wd-checkall').removeAttr('checked');
    $.post(localStorage.domain_app + "/webservice/guest/getEventInviteGuest.php", {
        page: 1,
        event_id: sessionStorage.event_edit_id,
        user_id: localStorage.userID
    }, function(res) {
        $('#wd-list-guest-event').html(res.html);
        $('#page').val(res.page);
        $('#end_paging').val(res.end_paging);
        $('.wd-loading').hide();
        checkAll();
        eventClickCheckbox();
    }, "json");
}
